<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>验证回文串</title>
</head>
<body>
  <script>
    var s = 'abca'
    function isPalindrome(str, l, r) {
      while (l < r) {  //对撞指针不断判断两边的数字是否相等         
        if (str[l] != str[r]) {
          return false;
        }
        l++;
        r--;
      }
      return true;
    }

    function fn(str) {
      let r = str.length - 1; //头尾指针
      for(let l = 0; l < r; l++,r--) {
        // console.log(str[l], str[r])
        if (str[l] != str[r]) {//左右指针不一样 还有一次机会，左指针向前一步或者右指针向后一步继续验证
          return isPalindrome(str, l + 1, r) || isPalindrome(str, l, r - 1);
        }
      }
      return true;
    };
    console.log(fn(s))
  </script>
</body>
</html>